//
//  NSDictionary+WOCrash.m
//  GridGovernance
//
//  Created by 吴欧 on 2017/12/21.
//  Copyright © 2017年 Bitvalue. All rights reserved.
//

#import "NSDictionary+WOCrash.h"
#import "NSObject+WOSwizzle.h"

@implementation NSDictionary (WOCrash)

+ (void)wo_enableDictionaryProtector {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    //__NSPlaceholderDictionary
        [self wo_classSwizzleMethodWithClass:self orginalMethod:@selector(dictionaryWithObjects:forKeys:count:) replaceMethod:@selector(wo_dictionaryWithObjects:forKeys:count:)];
    });
}
+ (instancetype)wo_dictionaryWithObjects:(const id  _Nonnull __unsafe_unretained *)objects forKeys:(const id<NSCopying>  _Nonnull __unsafe_unretained *)keys count:(NSUInteger)cnt{

    id instance = nil;
    @try{
       instance = [self wo_dictionaryWithObjects:objects forKeys:keys count:cnt];
    }
    @catch (NSException *exception){

         NSString *defaultToDo = @"This framework default is to remove nil key-values and instance a dictionary.";
        [WOCrashLog wo_noteErrorWithException:exception attachedTODO:defaultToDo];

      //处理错误的数据，然后重新初始化一个字典
        NSUInteger index = 0;
        id  _Nonnull __unsafe_unretained newObjects[cnt];
        id  _Nonnull __unsafe_unretained newkeys[cnt];

        for (int i = 0; i < cnt; i++){
            if (objects[i] && keys[i]){
                newObjects[index] = objects[i];
                newkeys[index] = keys[i];
                index++;
         }
      }
      instance = [self wo_dictionaryWithObjects:newObjects forKeys:newkeys count:index];
  }
    @finally{
      return instance;
    }
}
@end
